<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="所属职级" prop="StaffPostRole">
        <el-select
          v-model="form.StaffPostRole"
          style="width: 150px"
          @change="getStaffPost"
        >
          <el-option
            v-for="item in StaffPostRoleSelectList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="岗位名称" prop="StaffPostName">
        <el-input v-model="form.StaffPostName" />
      </el-form-item>
      <el-form-item label="岗位说明" prop="StaffPostMemo">
        <el-input v-model="form.StaffPostMemo" />
      </el-form-item>
      <el-form-item label="公海商机库容" prop="OpenSeaStockFull">
        <el-input v-model="form.OpenSeaStockFull" />
      </el-form-item>
      <el-form-item label="内海商机库容" prop="InSeaStockFull">
        <el-input v-model="form.InSeaStockFull" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import {
    saveStaffPost,
    StaffPostRoleSelect,
  } from '@/api/systemsetOrganizationStaffpost'

  export default defineComponent({
    name: 'SystemsetOrganizationStaffpostEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        StaffPostRoleSelectList: [],
        formRef: null,
        form: {
          StaffPostID: 0,
        },
        rules: {
          StaffPostRole: [
            { required: true, trigger: 'change', message: '请选择所属职级' },
          ],
          StaffPostName: [
            { required: true, trigger: 'blur', message: '请输入岗位名称' },
          ],
          StaffPostMemo: [
            { required: true, trigger: 'blur', message: '请输入更为说明' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      })

      const showEdit = (row) => {
        if (!row) {
          state.title = '添加'
        } else {
          state.title = '编辑'
          state.form = Object.assign({}, row)
          if (!state.form?.StaffPostID) {
            state.form.StaffPostID = 0
          }
        }
        staffPost()
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const staffPost = async () => {
        const {
          data: { StaffPostRoleOptions },
        } = await StaffPostRoleSelect()
        state.StaffPostRoleSelectList = StaffPostRoleOptions
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await saveStaffPost(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      return {
        ...toRefs(state),
        showEdit,
        staffPost,
        close,
        save,
      }
    },
  })
</script>
